package org.ytor.core.sysapi.codegen.service.support;

import org.ytor.core.sysapi.codegen.service.IConnectionService;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * created by yangtong on 2025/4/6 下午12:05
 * <br/>
 * 数据库连接操作实现类，基于连接池
 */
public class DefaultConnectionServiceImpl implements IConnectionService {

    private DataSource dataSource = null;

    public void injectDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return dataSource.getConnection(username, password);
    }

    @Override
    public void closeConnection(Connection connection) throws SQLException {
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }

    @Override
    public void closeResultSet(ResultSet set) throws SQLException {
        if (set != null && !set.isClosed()) {
            set.close();
        }
    }
}
